package csvm_v3;

import csvm_v1.exceptions.WrongCommandFormatException;
import csvm_v1.products.Product;
import csvm_v3.components.CoinContainer;
import csvm_v3.components.CoinDispenser;
import csvm_v3.components.CoinReceiver;
import csvm_v3.components.DumpFileReader;
import csvm_v3.components.DumpFileWriter;
import csvm_v3.components.ProductContainer;
import csvm_v3.components.ProductDispenser;
import csvm_v3.components.ProductSelector;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.PriceUtility;

/**
 *
 * @author Muhammad Anis
 */
public class VendingMachineFrame extends javax.swing.JFrame {

    /**
     * Input koin dari user
     * @return container input user
     */
    public CoinReceiver getCoinIn() {
        return coinIn;
    }

    /**
     * Setter Input koin dari user
     * @param coinIn kontainer input user
     */
    public void setCoinIn(CoinReceiver coinIn) {
        this.coinIn = coinIn;
    }

    /**
     * Getter kontainer untuk kembalian vending machine
     * @return kontainer untuk kembalian vending machine
     */
    public CoinContainer getCoinStock() {
        return coinStock;
    }

    /**
     * Setter kontainer untuk kembalian vending machine    
     * @param coinStock kontainer kembalian vending machine
     */
    public void setCoinStock(CoinContainer coinStock) {
        this.coinStock = coinStock;
    }

    
    /**
     * @return Part yang mengembalikan koin untuk user
     */
    public CoinDispenser getCoinOut() {
        return coinOut;
    }

    /**
     * 
     * @param coinOut Part yang mengembalikan koin untuk user
     */
    public void setCoinOut(CoinDispenser coinOut) {
        this.coinOut = coinOut;
    }

    
    /**
     * Mengembalikan List produk yang tersedia pada vending machine
     * @return daftar produk yang tersedia
     */
    public ProductContainer getProductStock() {
        return productStock;
    }

    /**
     * Mengeset list produk yang tersedia pada vending machine
     * @param productStock daftar produk yang tersedia
     */
    public void setProductStock(ProductContainer productStock) {
        this.productStock = productStock;
    }

    /**
     * Mengembalikan Part dari Vending Machine yang mengeluarkan produk yang telah dipilih oleh user
     * @return Part dari Vending Machine yang mengeluarkan produk yang telah dipilih oleh user
     */
    public ProductDispenser getProductOut() {
        return productOut;
    }

    /**
     * Mengeset Part dari Vending Machine yang mengeluarkan produk yang telah dipilih oleh user
     * @param productOut Part dari Vending Machine yang mengeluarkan produk yang telah dipilih oleh user
     */
    public void setProductOut(ProductDispenser productOut) {
        this.productOut = productOut;
    }

    
    /**
     * Mengembalikan Daftar ketersediaan produk
     * @return Daftar ketersediaan produk
     */
    public ProductSelector getCatalog() {
        return catalog;
    }

    /**
     * Mengeset Daftar ketersediaan produk
     * @param catalog Daftar ketersediaan produk
     */
    public void setCatalog(ProductSelector catalog) {
        this.catalog = catalog;
    }

    /**
     * Reader untuk load data Vending Machine dari File
     * @return Reader untuk load data Vending Machine dari File
     */
    public DumpFileReader getReader() {
        return reader;
    }

    /**
     * Mengeset Reader untuk load data Vending Machine dari File
     * @param reader Reader untuk load data Vending Machine dari File
     */
    public void setReader(DumpFileReader reader) {
        this.reader = reader;
    }

    /**
     * Mengambil writer untuk save data Vending Machine ke File
     * @return writer untuk save data Vending Machine ke File
     */
    public DumpFileWriter getWriter() {
        return writer;
    }

    /**
     * Mengeset writer untuk save data Vending Machine ke File
     * @param writer writer untuk save data Vending Machine ke File
     */
    public void setWriter(DumpFileWriter writer) {
        this.writer = writer;
    }

    /**
     * Mendapatkan path file yang mengandung data vending Machine
     * @return path file yang mengandung data vending Machine
     */
    public String getFilePath() {
        return filePath;
    }

    /**
     * Mengeset path file yang mengandung data vending Machine
     * 
     * @param filePath path file yang mengandung data vending Machine
     */
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    
    /**
     * Kontainer input koin dari user
     */
    private CoinReceiver coinIn;
    /**
     * Kontainer koin untuk kembalian
     */
    private CoinContainer coinStock;
    /**
     * Part untuk mengembalikan koin
     */
    private CoinDispenser coinOut;
    /**
     * Data ketersediaan produk
     */
    private ProductContainer productStock;
    /**
     * Part untuk mengembalikan produk
     */
    private ProductDispenser productOut;
    /**
     * Data produk yang tersedia
     */
    private ProductSelector catalog;
    /**
     * Loader data Vending Machine dari File
     */
    private DumpFileReader reader;
    /**
     * Writer data dari Vending Machine ke File
     */
    private DumpFileWriter writer;
    /**
     * 
     */
    private String filePath;

    /**
     * Creates new form VendingMachineFrame
     */
    public VendingMachineFrame(String filePath) {
        initComponents();
        this.filePath = filePath;
        coinIn = new CoinReceiver();
        coinStock = new CoinContainer();
        coinOut = new CoinDispenser(coinStock);
        productStock = new ProductContainer();
        productOut = new ProductDispenser();
        catalog = new ProductSelector();
        reader = new DumpFileReader(coinStock, productStock, catalog);
        writer = new DumpFileWriter(coinStock, productStock, catalog);
        initVM();
    }

    private void initVM() {
        try {
            reader.importDump(filePath);
            coinIn.emptyCoins();
            initProductName();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(VendingMachineFrame.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(0);
        } catch (WrongCommandFormatException ex) {
            Logger.getLogger(VendingMachineFrame.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(0);
        }
    }

    private void closeVM() {
        writer.exportDump(filePath);
        System.exit(0);
    }

    private void initProductName() {
        for (Integer i : catalog.getCatalog().keySet()) {
            Product p = catalog.getCatalog().get(i);
            setProductDesc(i, p);
        }
    }

    private void setProductDesc(int i, Product p) {
        switch (i) {
            case 1:
                produk_1.setText(p.getProductName());
                count_produk_1.setText(productStock.getProductStock().get(p)+"");
                break;
            case 2:
                produk_2.setText(p.getProductName());
                count_produk_2.setText(productStock.getProductStock().get(p)+"");
                break;                
            case 3:
                produk_3.setText(p.getProductName());
                count_produk_3.setText(productStock.getProductStock().get(p)+"");
                break;
            case 4:
                produk_4.setText(p.getProductName());
                count_produk_4.setText(productStock.getProductStock().get(p)+"");
                break;
            case 5:
                produk_5.setText(p.getProductName());
                count_produk_5.setText(productStock.getProductStock().get(p)+"");
                break;
            case 6:
                produk_6.setText(p.getProductName());
                count_produk_6.setText(productStock.getProductStock().get(p)+"");
                break;
            case 7:
                produk_7.setText(p.getProductName());
                count_produk_7.setText(productStock.getProductStock().get(p)+"");
                break;
            case 8:
                produk_8.setText(p.getProductName());
                count_produk_8.setText(productStock.getProductStock().get(p)+"");
                break;
            case 9:
                produk_9.setText(p.getProductName());
                count_produk_9.setText(productStock.getProductStock().get(p)+"");
                break;
            case 10:
                produk_10.setText(p.getProductName());
                count_produk_10.setText(productStock.getProductStock().get(p)+"");
                break;
            default:
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        produk_1 = new javax.swing.JLabel();
        produk_2 = new javax.swing.JLabel();
        produk_3 = new javax.swing.JLabel();
        produk_4 = new javax.swing.JLabel();
        produk_5 = new javax.swing.JLabel();
        produk_6 = new javax.swing.JLabel();
        produk_7 = new javax.swing.JLabel();
        produk_8 = new javax.swing.JLabel();
        produk_9 = new javax.swing.JLabel();
        produk_10 = new javax.swing.JLabel();
        count_produk_1 = new javax.swing.JLabel();
        count_produk_2 = new javax.swing.JLabel();
        count_produk_3 = new javax.swing.JLabel();
        count_produk_4 = new javax.swing.JLabel();
        count_produk_5 = new javax.swing.JLabel();
        count_produk_6 = new javax.swing.JLabel();
        count_produk_7 = new javax.swing.JLabel();
        count_produk_8 = new javax.swing.JLabel();
        count_produk_9 = new javax.swing.JLabel();
        count_produk_10 = new javax.swing.JLabel();
        btn_produk_1 = new javax.swing.JButton();
        btn_produk_2 = new javax.swing.JButton();
        btn_produk_3 = new javax.swing.JButton();
        btn_produk_4 = new javax.swing.JButton();
        btn_produk_5 = new javax.swing.JButton();
        btn_produk_6 = new javax.swing.JButton();
        btn_produk_7 = new javax.swing.JButton();
        btn_produk_8 = new javax.swing.JButton();
        btn_produk_9 = new javax.swing.JButton();
        btn_produk_10 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btn_insert_nickel = new javax.swing.JButton();
        btn_insert_dime = new javax.swing.JButton();
        btn_insert_quarter = new javax.swing.JButton();
        btn_insert_slug = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txt_product_dispense = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txt_total_money = new javax.swing.JLabel();
        txt_total_change = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btn_cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CS Vending Machine");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        produk_1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_1.setText("Produk 1");
        produk_1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_2.setText("Produk 2");
        produk_2.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_3.setText("Produk 3");
        produk_3.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_4.setText("Produk 4");
        produk_4.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_5.setText("Produk 5");
        produk_5.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_6.setText("Produk 6");
        produk_6.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_7.setText("Produk 7");
        produk_7.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_8.setText("Produk 8");
        produk_8.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_8.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_9.setText("Produk 9");
        produk_9.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_9.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        produk_10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        produk_10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        produk_10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/clouds_back.png"))); // NOI18N
        produk_10.setText("Produk 10");
        produk_10.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        produk_10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        produk_10.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        count_produk_1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_1.setText("0");
        count_produk_1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_2.setText("0");
        count_produk_2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_3.setText("0");
        count_produk_3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_4.setText("0");
        count_produk_4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_5.setText("0");
        count_produk_5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_6.setText("0");
        count_produk_6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_7.setText("0");
        count_produk_7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_8.setText("0");
        count_produk_8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_9.setText("0");
        count_produk_9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        count_produk_10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        count_produk_10.setText("0");
        count_produk_10.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        btn_produk_1.setText("Pilih");
        btn_produk_1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_1ActionPerformed(evt);
            }
        });

        btn_produk_2.setText("Pilih");
        btn_produk_2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_2ActionPerformed(evt);
            }
        });

        btn_produk_3.setText("Pilih");
        btn_produk_3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_3ActionPerformed(evt);
            }
        });

        btn_produk_4.setText("Pilih");
        btn_produk_4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_4ActionPerformed(evt);
            }
        });

        btn_produk_5.setText("Pilih");
        btn_produk_5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_5ActionPerformed(evt);
            }
        });

        btn_produk_6.setText("Pilih");
        btn_produk_6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_6ActionPerformed(evt);
            }
        });

        btn_produk_7.setText("Pilih");
        btn_produk_7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_7ActionPerformed(evt);
            }
        });

        btn_produk_8.setText("Pilih");
        btn_produk_8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_8ActionPerformed(evt);
            }
        });

        btn_produk_9.setText("Pilih");
        btn_produk_9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_9ActionPerformed(evt);
            }
        });

        btn_produk_10.setText("Pilih");
        btn_produk_10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_produk_10ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Please Pick Your Product Here");

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 236, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 18, Short.MAX_VALUE)
        );

        btn_insert_nickel.setText("Insert Nickel");
        btn_insert_nickel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_insert_nickelActionPerformed(evt);
            }
        });

        btn_insert_dime.setText("Insert Dime");
        btn_insert_dime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_insert_dimeActionPerformed(evt);
            }
        });

        btn_insert_quarter.setText("Insert Quarter");
        btn_insert_quarter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_insert_quarterActionPerformed(evt);
            }
        });

        btn_insert_slug.setText("Insert Slug");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));

        txt_product_dispense.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        txt_product_dispense.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(txt_product_dispense, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(75, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(txt_product_dispense, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("CS Vending Machine");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Total Money : ");

        txt_total_money.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txt_total_money.setText("0");

        txt_total_change.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txt_total_change.setText("0");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Total Change   : ");

        jPanel3.setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 236, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 18, Short.MAX_VALUE)
        );

        btn_cancel.setText("Cancel");
        btn_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(produk_1)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(btn_produk_1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addGap(29, 29, 29)
                                            .addComponent(count_produk_1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(produk_2)
                                        .addGap(18, 18, 18)
                                        .addComponent(produk_3)
                                        .addGap(18, 18, 18)
                                        .addComponent(produk_4)
                                        .addGap(18, 18, 18)
                                        .addComponent(produk_5))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(37, 37, 37)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(btn_produk_2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(count_produk_2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(71, 71, 71)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(count_produk_3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(count_produk_4, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(btn_produk_3, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btn_produk_4, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(71, 71, 71)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(count_produk_5, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(btn_produk_5, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(14, 14, 14))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(produk_6)
                                .addGap(18, 18, 18)
                                .addComponent(produk_7)
                                .addGap(18, 18, 18)
                                .addComponent(produk_8)
                                .addGap(18, 18, 18)
                                .addComponent(produk_9)
                                .addGap(18, 18, 18)
                                .addComponent(produk_10)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(count_produk_6, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(79, 79, 79))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(btn_produk_6, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(74, 74, 74)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btn_produk_7, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(count_produk_7, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(55, 55, 55)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(count_produk_8, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btn_produk_8, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(76, 76, 76)
                                        .addComponent(count_produk_9, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(count_produk_10, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btn_produk_9, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(71, 71, 71)
                                        .addComponent(btn_produk_10, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(31, 31, 31))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(67, 67, 67)
                                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txt_total_change)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(83, 83, 83))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(btn_insert_nickel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btn_insert_dime)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btn_insert_quarter))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txt_total_money)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(btn_cancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btn_insert_slug, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addContainerGap())))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(0, 0, Short.MAX_VALUE))))))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(317, 317, 317)
                    .addComponent(jLabel2)
                    .addContainerGap(351, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(97, 97, 97)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(produk_5, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(produk_4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(produk_3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(produk_2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(produk_1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(count_produk_1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(count_produk_2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(count_produk_3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(count_produk_4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(count_produk_5, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_produk_1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(87, 87, 87)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(produk_10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(produk_9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(produk_7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(produk_6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(produk_8, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(count_produk_6, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(count_produk_8, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(count_produk_7, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(count_produk_10, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(count_produk_9, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_produk_6, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_8, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_7, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_9, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_produk_10, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(73, 73, 73)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_insert_nickel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_insert_dime, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_insert_quarter, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_insert_slug, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(txt_total_money)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txt_total_change))))
                .addContainerGap(22, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(35, 35, 35)
                    .addComponent(jLabel2)
                    .addContainerGap(658, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_insert_dimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_insert_dimeActionPerformed
        // TODO add your handling code here:
        coinIn.setDimeCount(coinIn.getDimeCount() + 1);
        coinStock.setDimeCount(coinStock.getDimeCount() + 1);
        updateTotalMoneyText();
    }//GEN-LAST:event_btn_insert_dimeActionPerformed

    private void btn_insert_nickelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_insert_nickelActionPerformed
        // TODO add your handling code here:
        //insert nickel to coin in coin stock
        coinIn.setNickelCount(coinIn.getNickelCount() + 1);
        coinStock.setNickelCount(coinStock.getNickelCount() + 1);
        updateTotalMoneyText();
    }//GEN-LAST:event_btn_insert_nickelActionPerformed

    private void btn_insert_quarterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_insert_quarterActionPerformed
        // TODO add your handling code here:
        //insert quarter to coin in coin stock
        coinIn.setQuarterCount(coinIn.getQuarterCount() + 1);
        coinStock.setQuarterCount(coinStock.getQuarterCount() + 1);
        updateTotalMoneyText();
    }//GEN-LAST:event_btn_insert_quarterActionPerformed

    private void btn_produk_1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_1ActionPerformed
        // TODO add your handling code here:
        //pilih produk dengan kode 1   
        buyProduct(1);
    }//GEN-LAST:event_btn_produk_1ActionPerformed

    private void btn_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelActionPerformed
        // TODO add your handling code here:
        if(coinIn.getDimeCount() != 0 || coinIn.getNickelCount() != 0 || coinIn.getQuarterCount() != 0){
            coinOut.giveChange(PriceUtility.CoinToPrice(coinIn.getNickelCount(), coinIn.getDimeCount(), coinIn.getQuarterCount()), txt_total_change);
            txt_product_dispense.setText("Please Take your Money");
        }
        coinIn.emptyCoins();
    }//GEN-LAST:event_btn_cancelActionPerformed

    private void btn_produk_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_2ActionPerformed
        // TODO add your handling code here:
        buyProduct(2);
    }//GEN-LAST:event_btn_produk_2ActionPerformed

    private void btn_produk_3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_3ActionPerformed
        // TODO add your handling code here:
        buyProduct(3);
    }//GEN-LAST:event_btn_produk_3ActionPerformed

    private void btn_produk_4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_4ActionPerformed
        // TODO add your handling code here:
        buyProduct(4);
    }//GEN-LAST:event_btn_produk_4ActionPerformed

    private void btn_produk_5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_5ActionPerformed
        // TODO add your handling code here:
        buyProduct(5);
    }//GEN-LAST:event_btn_produk_5ActionPerformed

    private void btn_produk_6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_6ActionPerformed
        // TODO add your handling code here:
        buyProduct(6);
    }//GEN-LAST:event_btn_produk_6ActionPerformed

    private void btn_produk_7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_7ActionPerformed
        // TODO add your handling code here:
        buyProduct(7);
    }//GEN-LAST:event_btn_produk_7ActionPerformed

    private void btn_produk_8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_8ActionPerformed
        // TODO add your handling code here:
        buyProduct(8);
    }//GEN-LAST:event_btn_produk_8ActionPerformed

    private void btn_produk_9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_9ActionPerformed
        // TODO add your handling code here:
        buyProduct(9);
    }//GEN-LAST:event_btn_produk_9ActionPerformed

    private void btn_produk_10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_produk_10ActionPerformed
        // TODO add your handling code here:
        buyProduct(10);
    }//GEN-LAST:event_btn_produk_10ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        writer.exportDump(filePath);
    }//GEN-LAST:event_formWindowClosing

    private void buyProduct(int pilihan) {
        if (catalog.getCatalog().containsKey(pilihan)) {
            Product produk = (Product) catalog.getCatalog().get(pilihan);
            int totalBayar = produk.getPrice();
            if (totalBayar <= PriceUtility.CoinToPrice(coinIn.getNickelCount(), coinIn.getDimeCount(), coinIn.getQuarterCount())) {
                doTransaction(produk, 1);
            } else {
                txt_product_dispense.setText("Not enough Money");
            }
        }
        updateTotalMoneyText();
    }

    private void doTransaction(Product produk, int jumlah) {
        int totalMoney = PriceUtility.CoinToPrice(coinIn.getNickelCount(), coinIn.getDimeCount(), coinIn.getQuarterCount());
        int lastProductCount = productStock.getProductStock().get(produk);
        if (lastProductCount > 0) {
            totalMoney -= produk.getPrice();
            if (coinOut.giveChange(totalMoney, txt_total_change)) {
                productStock.giveProducts(produk, 1);
                updateTxtProductDispense(produk);
                initProductName();
            } else {
                txt_product_dispense.setText("Sorry, insuificient changes for you");
                totalMoney = PriceUtility.CoinToPrice(coinIn.getNickelCount(), coinIn.getDimeCount(), coinIn.getQuarterCount());
                coinOut.giveChange(totalMoney, txt_total_change);
            }
        } else {
            txt_product_dispense.setText("No Product Available");
            coinOut.giveChange(totalMoney, txt_total_change);
        }
        coinIn.emptyCoins();
    }

    private void updateTxtProductDispense(Product produk) {
        txt_product_dispense.setText(produk.getProductName());
    }

    private void updateTotalMoneyText() {
        txt_total_money.setText(PriceUtility.CoinToPrice(coinIn.getNickelCount(), coinIn.getDimeCount(), coinIn.getQuarterCount()) + "");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VendingMachineFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VendingMachineFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VendingMachineFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VendingMachineFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VendingMachineFrame("vm_dump.txt").setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_cancel;
    private javax.swing.JButton btn_insert_dime;
    private javax.swing.JButton btn_insert_nickel;
    private javax.swing.JButton btn_insert_quarter;
    private javax.swing.JButton btn_insert_slug;
    private javax.swing.JButton btn_produk_1;
    private javax.swing.JButton btn_produk_10;
    private javax.swing.JButton btn_produk_2;
    private javax.swing.JButton btn_produk_3;
    private javax.swing.JButton btn_produk_4;
    private javax.swing.JButton btn_produk_5;
    private javax.swing.JButton btn_produk_6;
    private javax.swing.JButton btn_produk_7;
    private javax.swing.JButton btn_produk_8;
    private javax.swing.JButton btn_produk_9;
    private javax.swing.JLabel count_produk_1;
    private javax.swing.JLabel count_produk_10;
    private javax.swing.JLabel count_produk_2;
    private javax.swing.JLabel count_produk_3;
    private javax.swing.JLabel count_produk_4;
    private javax.swing.JLabel count_produk_5;
    private javax.swing.JLabel count_produk_6;
    private javax.swing.JLabel count_produk_7;
    private javax.swing.JLabel count_produk_8;
    private javax.swing.JLabel count_produk_9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel produk_1;
    private javax.swing.JLabel produk_10;
    private javax.swing.JLabel produk_2;
    private javax.swing.JLabel produk_3;
    private javax.swing.JLabel produk_4;
    private javax.swing.JLabel produk_5;
    private javax.swing.JLabel produk_6;
    private javax.swing.JLabel produk_7;
    private javax.swing.JLabel produk_8;
    private javax.swing.JLabel produk_9;
    private javax.swing.JLabel txt_product_dispense;
    private javax.swing.JLabel txt_total_change;
    private javax.swing.JLabel txt_total_money;
    // End of variables declaration//GEN-END:variables
}
